package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.Course;
import cn.xgj.sfzy.service.CourseService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CourseController
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月23日 下午8:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @RequestMapping("/findAll")
    public Result findAll(Course course){
        List<Course> list = courseService.findAll(course);
        return Result.success(list);
    }
    @RequestMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "3") Integer pageSize,
            Course course){
        PageInfo<Course> byPage = courseService.findByPage(pageNum, pageSize, course);
        return Result.success(byPage);
    }

    @PostMapping
    public Result add(@RequestBody Course course){
        int num = courseService.add(course);
        if(num!=0){
            return Result.success("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        int num = courseService.edit(course);
        if(num!=0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{courseId}")
    public Result delete(@PathVariable(value = "courseId") Integer courseId){
        int num = courseService.remove(courseId);
        if(num!=0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }
    @GetMapping("/findById")
    public Result findById(@Param("courseId") Integer courseId){
        System.out.println(courseId);
        Course course = courseService.findById(courseId);
        if(course==null){
            return Result.error("查找失败");
        }else{
            return Result.success(course);
        }
    }
}
